﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SecurityDev
{
    public partial class Settings : Form
    {
        string usser;
        public Settings(string user)
        {
            usser = user;
            InitializeComponent();
            Deserialize();
            
        }

        public void Deserialize()
        {
            List<string> words;
            FileStream fs = new FileStream("config/" + usser + ".s", FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryFormatter bf = new BinaryFormatter();
            words = (List<string>)bf.Deserialize(fs);
            fs.Close();
            foreach (string w in words)
            {
                if (w == "Subcriber")
                { Subcriber.Checked = true; }
                if (w == "Address")
                { Address.Checked = true; }
                if (w == "Date")
                { Date.Checked = true; }
                if (w == "DVRIP")
                { DVRIP.Checked = true; }
                if (w == "Email")
                { Email.Checked = true; }
                if (w == "ObjectName")
                { ObjectName.Checked = true; }
                if (w == "Phone")
                { Phone.Checked = true; }
                if (w == "Priority")
                { Priority.Checked = true; }

            }
        }

        public void Serialize(List<string> words)
        {
           
            FileStream fs = new FileStream("config/"+usser+".s", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();

            //сериализация
            bf.Serialize(fs, words);
            fs.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            List<string> columns = new List<string>();
            if (Subcriber.Checked == true) 
            {
                columns.Add(Subcriber.Name.ToString());
            }
             if (Address.Checked == true)
                {
                    columns.Add(Address.Name.ToString());
                }
             if (Date.Checked == true)
            {
                columns.Add(Date.Name.ToString());
            }
             if (DVRIP.Checked == true)
            {
                columns.Add(DVRIP.Name.ToString());
            }
             if (Email.Checked == true)
            {
                columns.Add(Email.Name.ToString());
            }
             if (ObjectName.Checked == true)
            {
                columns.Add(ObjectName.Name.ToString());
            }
             if (Phone.Checked == true)
            {
                columns.Add(Phone.Name.ToString());
            }
             if (Priority.Checked == true)
            {
                columns.Add(Priority.Name.ToString());
            }
             Serialize(columns);
             Settings.ActiveForm.Close();
        }
        
    }
}
